<?php
require_once(ABSPATH . '/wp-admin/upgrade-schema.php');

function slugAvailable($slug){ // returns true if slug is not in use
	global $wpdb;
	$slug_taken = $wpdb->get_row("SELECT slug FROM $wpdb->blogs WHERE slug = '".mysql_real_escape_string($slug)."'");
	return !$slug_taken;
}

function lyceum_create_blog_from_username($username, $blog_name){
	global $wpdb;
	$userid = $wpdb->get_var("SELECT `ID` FROM $wpdb->users WHERE `user_login` = '$username'");
	return lyceum_create_blog($userid, $blog_name);
}

function lyceum_create_blog ($userid, $blog_name){
	global $wpdb, $blog, $cache_settings, $table_prefix, $optiondomain, $wp_rewrite;
	$optiondomain = 'blog';
	$cache_settings = array(); //clear bogus blog=0 cache
	$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$userid'");
	assert('$user');
	if (1==$user->user_admin)
		die('The system admin user may not create or have specific privileges on a blog (this user already has all privileges on all blogs).');
	$wpdb->query("INSERT INTO $wpdb->blogs (created, slug) VALUES ( NOW( ), '$blog_name' )");
	$blog = $wpdb->insert_id;
	assert('isset($blog)');

	populate_options();
	populate_roles();
	unset($GLOBALS['wp_roles']);//need to do this for php4... not completely sure why. for some reason the wp_roles object needs to close down and then get fired back up again later in order for the roles to be visible...
	$wpdb->query("INSERT INTO $wpdb->usermeta (user_id, meta_key, meta_value, blog) VALUES ($userid, '{$table_prefix}user_level', '10', '$blog')");
	assert('get_usermeta($userid, $table_prefix.\'user_level\')');
	$wpuser = new WP_User($userid);
	$wpuser->set_role('administrator');
	update_option('blogname', $blog_name);
	update_option('admin_email', $user->user_email);
	
	// Now drop in some default links
	$wpdb->query("INSERT INTO $wpdb->linkcategories (cat_name, blog) VALUES ('".$wpdb->escape(__('Lyceum'))."', '$blog')");
	$linkcatid=$wpdb->insert_id;
	update_option('default_link_category', $linkcatid);
	$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_owner) VALUES ('http://lyceum.ibiblio.org/', 'Lyceum Project Blog', '$linkcatid', 'http://lyceum.ibiblio.org/feed/', '$userid');");
	$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_owner) VALUES ('http://ibiblio.org/ibiblog', 'Ibiblio Blog', '$linkcatid', 'http://www.ibiblio.org/ibiblog/feed/', '$userid');");
	$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_owner) VALUES ('http://blog.johnjosephbachir.org/', 'John', '$linkcatid', 'http://blog.johnjosephbachir.org/feed/', '$userid');");
	$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_owner) VALUES ('http://chimprawk.blogspot.com/', 'Fred', '$linkcatid', 'http://feeds.feedburner.com/chimprawk', '$userid');");
	$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_owner) VALUES ('http://www.ibiblio.org/pjones/wordpress/', 'Paul', '$linkcatid', 'http://ibiblio.org/pjones/wordpress/?feed=rss2', '$userid');");

	// Default category
	$wpdb->query("INSERT INTO $wpdb->categories (blog, cat_name, category_nicename, category_count) VALUES ('$blog', '".$wpdb->escape(__('Uncategorized'))."', '".sanitize_title(__('Uncategorized'))."', '1')");
	$catid=$wpdb->insert_id;
	update_option('default_category', $catid);
	// First post
	$now = date('Y-m-d H:i:s');
	$now_gmt = gmdate('Y-m-d H:i:s');

	$post_content = $wpdb->escape(__('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!'));
	$post_title = $wpdb->escape(__('Hello world!'));
	$wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_title, post_name, post_modified, post_modified_gmt, comment_count) VALUES ($userid, '$now', '$now_gmt', '".$post_content."', '".$post_title."', '".$wpdb->escape(__('hello-world'))."', '$now', '$now_gmt', '1')");
	$postid=$wpdb->insert_id;
	$wpdb->query( "INSERT INTO $wpdb->post2cat (`post_id`, `category_id`) VALUES ($postid, $catid)" );
	$wpdb->query( "UPDATE $wpdb->posts SET guid = '".get_permalink($postid)."' WHERE ID = '$postid'" );

	$wpdb->query("
    INSERT INTO $wpdb->postsearch (post_id, component, textdata)
		VALUES ('$postid', 'title', '$post_title')
  ");

	$wpdb->query("
    INSERT INTO $wpdb->postsearch (post_id, component, textdata)
		VALUES ('$postid', 'content', '$post_content')
  ");	

	// Default comment
	$comment_content = $wpdb->escape(__('Hi, this is a comment.<br />To delete a comment, just log in, and view the posts&#039; comments, there you will have the option to edit or delete them.'));
	$wpdb->query("INSERT INTO $wpdb->comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_date, comment_date_gmt, comment_content) VALUES ('$postid', '".$wpdb->escape(__('Mr WordPress'))."', '', 'http://wordpress.org', '$now', '$now_gmt', '".$comment_content."')");
	$commentid=$wpdb->insert_id;

	$wpdb->query("
    INSERT INTO $wpdb->postsearch (post_id, object_id, component, textdata)
		VALUES ('$postid', '$commentid', 'comment', '$comment_content')
  ");
	
	// First Page
	$page_title = $wpdb->escape(__('About'));
	$page_content = $wpdb->escape(__('This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.'));
	$wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_title, post_name, post_modified, post_modified_gmt, post_status, page_blog) VALUES ($userid, '$now', '$now_gmt', '".$page_content."', '".$page_title."', '".$wpdb->escape(__('about'))."', '$now', '$now_gmt', 'static', '$blog')");
	$pageid=$wpdb->insert_id;
	$wpdb->query( "UPDATE $wpdb->posts SET guid = '".get_permalink($pageid)."' WHERE ID = '$pageid'" );

	//temporary fix until i do more sophisticated hacking of class WP_Query
	$wpdb->query( "INSERT INTO $wpdb->post2cat (`post_id`, `category_id`) VALUES ($pageid, $catid)" );

	$wpdb->query("
    INSERT INTO $wpdb->postsearch (post_id, component, textdata)
		VALUES ('$pageid', 'title', '$page_title')
  ");

	$wpdb->query("
    INSERT INTO $wpdb->postsearch (post_id, component, textdata)
		VALUES ('$pageid', 'content', '$page_content')
  ");	

	$wp_rewrite->flush_rules();

	return $blog;
}
?>
